import numpy as np
import matplotlib.pyplot as plt


def plot_show():
    A = np.asarray((1, 2, 2, 4, 3, 6)).reshape((3, 2))
    print(A)
    x = np.arange(-5, 5, 0.2)
    y1 = 0.5 * (1 - x)
    y2 = 0.5 * (2 - x)
    y3 = 0.5 * (3 - x)
    plt.scatter(x, y1, color='red')
    plt.scatter(x, y2, color='green')
    plt.scatter(x, y3, color='blue')
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    b1 = np.matmul(A, np.asarray((x, y1)))
    b2 = np.matmul(A, np.asarray((x, y2)))
    b3 = np.matmul(A, np.asarray((x, y3)))
    ax.scatter(*b1, color='red')
    ax.scatter(*b2, color='green')
    ax.scatter(*b3, color='blue')
    plt.show()


def plot_inverse():
    A = np.asarray((1, 2, 2, 4, 3, 6)).reshape((3, 2))
    print(A)
    x = np.arange(-5, 5, 0.5)
    y1 = 0.5 * (1 - x)
    y2 = 0.5 * (2 - x)
    y3 = 0.5 * (3 - x)

    b1 = np.matmul(A, np.asarray((x, y1)))
    b2 = np.matmul(A, np.asarray((x, y2)))
    b3 = np.matmul(A, np.asarray((x, y3)))

    b4 = np.asarray([[1.5], [1.8], [9]])
    inv = np.linalg.pinv(A)
    x4 = np.matmul(inv, b4)
    b4p = np.matmul(A, x4)

    plt.scatter(x, y1, color='red')
    plt.scatter(x, y2, color='green')
    plt.scatter(x, y3, color='blue')
    plt.scatter(*x4, color='black')
    plt.plot([0, 4], [0, 8])
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')

    ax.plot([0, 3], [0, 6], [0, 9])
    ax.plot([b4[0][0], b4p[0][0]], [b4[1][0], b4p[1][0]], [b4[2][0], b4p[2][0]])
    ax.scatter(*b1, color='red')
    ax.scatter(*b2, color='green')
    ax.scatter(*b3, color='blue')
    ax.scatter(*b4, color='black')
    ax.scatter(*b4p, color='black')
    plt.show()
    print(b4, x4, b4p, b4 - b4p)


if __name__ == '__main__':
    plot_show()
    plot_inverse()
